import axios from 'axios'
export default {
    namespaced: true,
    state: () => ({
        list: []
    }),
    mutations: {
       stopAsync(state,payload) {
        state.list = payload.list
       } ,
       setIsAll(state,payload) {
        state.list.forEach(item => item.goods_state = payload)
       },
       goodsShop(state,payload) {
        state.list[state.list.findIndex(item => item.id == payload.id)].goods_state = payload.check
       }
    },
    actions: {
        asyncList(ctx,res) {
            axios.get('/api/cart').then(res => {
              ctx.commit('stopAsync', res.data)
            })
        }
    },
    getters: {
        getIsAll(state) {
            if (state.list.length === 0) return false
            return state.list.every(item => item.goods_state)
        },
        allCount(state) {
            let sum = 0
            state.list.forEach(item => {
              if (item.goods_state) {
                sum += item.goods_count
              }
            })
            return sum
        },
        allPrice(state) {
            let sum = 0
            state.list.forEach(item => {
              if (item.goods_state) {
                sum += item.goods_count * item.goods_price
              }
            })
            return sum
        }
    }
}